package net.luoran.dvm.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.luoran.dvm.common.base.Result;
import net.luoran.dvm.common.util.CiphertextUtil;
import net.luoran.dvm.entity.PanelDO;
import net.luoran.dvm.entity.PanelModuleDO;
import net.luoran.dvm.entity.PanelReleaseDO;
import net.luoran.dvm.entity.ReleaseInfo;
import net.luoran.dvm.entity.vo.PanelReleaseVO;
import net.luoran.dvm.service.PanelModuleService;
import net.luoran.dvm.service.PanelReleaseService;
import net.luoran.dvm.service.PanelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author ：M1nG
 * @date ：Created in 2020/11/22 16:05
 * @description：
 * @version: 1.0
 */
@RestController
@RequestMapping("/release")
public class PanelReleaseController {

    @Autowired
    private PanelReleaseService panelReleaseService;

    @Autowired
    private PanelService panelService;

    @Autowired
    private PanelModuleService panelModuleService;

    @RequestMapping("/initToken")
    //@LoginToken
    public Result initToken(@RequestParam("id") String panelId, @RequestParam("code") String code, @RequestParam("time") String expireTime, @RequestParam("repeatMinute") Integer repeatMinute) {
        Result result = Result.ok();
        try {
            String uuid = IdUtil.simpleUUID();
            PanelReleaseDO panelReleaseDO = new PanelReleaseDO();
            panelReleaseDO.setReleasedId(uuid);
            panelReleaseDO.setPanelId(panelId);
            panelReleaseDO.setSecurityCode(code);
            if (expireTime.equals("-1")) {
                expireTime = LocalDate.now().plusYears(500).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            }
            ReleaseInfo releaseInfo = new ReleaseInfo(uuid, expireTime.replace("-",""), repeatMinute);
            String token = new CiphertextUtil(code).encrypt(releaseInfo.toStr());
            panelReleaseService.save(panelReleaseDO);
            result.setData(token);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("系统错误");
        }
        return result;
    }

    @RequestMapping("/getData")
    public Result getData(@RequestParam("token") String token, @RequestParam("code") String code) {
        Result result = Result.ok();
        try {
            CiphertextUtil ciphertextUtil = new CiphertextUtil(code);
            String decrypt = ciphertextUtil.decrypt(token);
            ReleaseInfo releaseInfo = new ReleaseInfo().init(decrypt);
            QueryWrapper<PanelReleaseDO> queryWrapper2Release = new QueryWrapper<>();
            queryWrapper2Release.eq("released_id", releaseInfo.getId());
            PanelReleaseDO release = panelReleaseService.getOne(queryWrapper2Release);
            LocalDate expireTime = LocalDate.parse(releaseInfo.getExpireTime(), DateTimeFormatter.ofPattern("yyyyMMdd"));
            if (LocalDate.now().isAfter(expireTime)) {
                return result.build("403", "有效期已过期！");
            }
            if (!code.equals(release.getSecurityCode())) {
                return result.build("403", "校验码不统一！");
            }
            QueryWrapper<PanelDO> queryWrapper2Panel = new QueryWrapper<>();
            queryWrapper2Panel.eq("panel_id", release.getPanelId());
            PanelDO panel = panelService.getOne(queryWrapper2Panel);
            QueryWrapper<PanelModuleDO> queryWrapper2PanelModule = new QueryWrapper<>();
            queryWrapper2PanelModule.eq("panel_id", panel.getPanelId());
            List<PanelModuleDO> panelModuleList = panelModuleService.list(queryWrapper2PanelModule);
            panel.setChildren(panelModuleList);
            PanelReleaseVO panelReleaseVO = new PanelReleaseVO(panel);
            panelReleaseVO.setRepeatMinute(releaseInfo.getRepeatMinute());
            result.setData(panelReleaseVO);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("系统错误");
        }
        return result;
    }
}
